<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionCore\Other;

use AuctionCore\System\ListBase;

/**
 * 门店列表
 * @author       xmh
 */
class StoreList extends ListBase {

    private $base_sql = null;
    private $count_sql = null;

    public function __construct() {
        $query_sql = "SELECT * ";
        $sql = " FROM au_store ";
        $this->base_sql = $query_sql . $sql;
        $this->count_sql = "select count(*) count " . $sql;
    }

    public function getList($page, $limit, $params) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $limit = (int) $limit < 1 ? 10 : (int) $limit;
        //搜索 
        $map = array();
        $map['status'] = array('eq', 0);

        //门店名称
        if (!empty($params['store_name'])) {
            $map['store_name'] = array('eq', $params['store_name']);
        }
        //门店名称
        if (!empty($params['type'])) {
            $map['type'] = array('eq', $params['type']);
        }
        //门店地址
        if (!empty($params['address'])) {
            $map['address'] = array('like', $params['address']);
        }
        //要查询的列
        $cols = "store_id,store_name,store_lead,store_tel,province,city,area,address,createtime,lasttime,status,`type`,service,pointer_x,pointer_y,qrcode,script";
        $total = M('Store')->alias('s')
                        ->where($map)->count();
        $list = M('Store')->alias('s')->field($cols)
                        ->where($map)->order("store_id")->limit($limit)->page($page)->select();
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

    /**
     * 获取列表结果
     * @param    int $ev_page_num    每页条数
     * @param    int $page    页码
     * @return   Object
     */
    public function get($ev_page_num, $page) {
        if (!$this->order_by) {
            $this->order_by = 'ORDER BY createtime ASC';
        }
        $store_id = $this->store_id;
        $Model = new \Think\Model();

        //门店列表信息
        $sql = $this->base_sql;
        $sql = str_replace('#order_by#', $this->order_by, $sql);
        $sql = str_replace('#condition#', $this->condition, $sql);
        $skip = ($page < 2 ? 0 : $ev_page_num * ($page - 1));
        $limit = " limit {$skip},{$ev_page_num}";
        $sql = str_replace('#limit#', $limit, $sql);
        $sql .= ";";

        $list = $Model->query($sql);

        return $list;
    }

    public function count() {

        $Model = new \Think\Model();
        $sql = $this->count_sql;
        $sql .= " {$this->condition}";
        $sql .= ";";
        $r = $Model->query($sql);
        return $r[0]['count'];
    }
    //获取门店详情
    public function detail($map=array()){
        return M('Store')->alias('s')->where($map)->find();
    }
    //获取门店列表
    public function getStoreList($page=1, $limit=0, $params=array()) {
        $page = (int) $page < 1 ? 1 : (int) $page;
        $limit = (int) $limit < 1 ? 10 : (int) $limit;
        //搜索
        $map = array();
        $map['status'] = array('eq', 1);
        if($params['id']>0){
            $map['store_id'] = array('eq',$params['id']);
        }
        $map['type'] = array('eq',($params['type'] ? $params['type'] : 1));

        //要查询的列
        $cols = "*";
        $total = M('store_pics')->alias('s')
                        ->where($map)->count();
        if($limit>0){
            $list = M('store_pics')->alias('s')->field($cols)
                            ->where($map)->order("createtime desc")->limit($limit)->page($page)->select();
        }else{
            $list = M('store_pics')->alias('s')->field($cols)
                            ->where($map)->order("createtime desc")->select();
        }
        $result = array(
            'list' => $list,
            'total' => $total
        );
        return $result;
    }

}
